//
//  PUTableView.m
//  PicUploader
//
//  Created by Chen Zhang on 8/30/08.
//  Copyright 2008 overboming. All rights reserved.
//

#import "PUTableView.h"


@implementation PUTableView
- (void) awakeFromNib
{
//    dragTypes = [NSArray arrayWithArray:typearray];
    [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType,NSTIFFPboardType, nil]];
	NSLog(@"register D&D");
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
	NSLog(@"entered");
    // Need the delegate hooked up to accept the dragged item(s) into the model
    if ([self delegate]==nil)
    {
        return NSDragOperationNone;
    }

    if ([[[sender draggingPasteboard] types] containsObject:NSFilenamesPboardType])
    {
        
        return NSDragOperationCopy;
    }  
    
    if ([[[sender draggingPasteboard] types] containsObject:NSTIFFPboardType])
    {
        
        return NSDragOperationCopy;
    }  
    
    return NSDragOperationNone;
}

// Work around a bug from 10.2 onwards
- (unsigned int)draggingSourceOperationMaskForLocal:(BOOL)isLocal
{
    return NSDragOperationEvery;
}

// Stop the NSTableView implementation getting in the way
- (NSDragOperation)draggingUpdated:(id <NSDraggingInfo>)sender
{
    return [self draggingEntered:sender];
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    int i;
    NSPasteboard *pboard;
    pboard = [sender draggingPasteboard];
    if ([[pboard types] containsObject:NSFilenamesPboardType])
    {
        id delegate = [self delegate];
        NSArray *filenames = [pboard propertyListForType:NSFilenamesPboardType];
        if ([delegate respondsToSelector:@selector(acceptDragOperation:)])
        {
            for (i=0;i<[filenames count];i++)
            {
                [delegate performSelector:@selector(acceptDragOperation:) withObject:[filenames objectAtIndex:i]];
				NSLog(@"should work");
            }
        }
        return YES;
    }
    
    if([[pboard types] containsObject:NSTIFFPboardType]){
        id delegate = [self delegate];
        if ([delegate respondsToSelector:@selector(acceptDragOperationForDraggedImage:)])
        {
            [delegate performSelector:@selector(acceptDragOperationForDraggedImage:) withObject:[pboard dataForType:NSTIFFPboardType]];
        }
        return YES;
    }
    
    return NO;
}

@end
